package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.cmcc.intertelecom.bean.Module;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.Power;
import cn.cmcc.intertelecom.bean.PowerGroup;
import cn.cmcc.intertelecom.bean.RoleGroup;
import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.service.LoginService;
import cn.cmcc.intertelecom.service.ModuleService;
import cn.cmcc.intertelecom.service.PowerGroupService;
import cn.cmcc.intertelecom.service.PowerService;
import cn.cmcc.intertelecom.service.RoleGroupService;

@SessionAttributes(names={"loginUser","listModule"})
//listModule
@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	@Autowired
	private RoleGroupService roleGroupService;
	@Autowired
	private PowerGroupService powerGroupService;
	@Autowired
	private PowerService powerService;
	@Autowired
	private ModuleService moduleService;

	/**
	 * 登录系统
	 * @param user
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkUser",method=RequestMethod.GET)
	public Msg checkUser(User user,Map<String,Object>map){
	
		User loginUser = loginService.checkUser(user);
		if (loginUser !=null) {
			//将user放在域中
			map.put("loginUser", loginUser);
			List<Module> listModule;
			if(loginUser.getUserType().equals("0")){
				//管理员用户 拥有所以权限
				//获取所有的模块
			  	listModule = moduleService.selectAllModule();
			}else{
				//普通用户 
				//根据用户id 获取角色-用户映射表中 关于此用户的数据
			  	List<RoleGroup> roleGroup = roleGroupService.getRoleByUserId(loginUser.getId());
			  	List<Integer> roleIds=new ArrayList<>();
			  	for (RoleGroup r : roleGroup) {
			  	   roleIds.add(r.getRoleId());
			  	}
			  	List<String> powers =new ArrayList<>();
			  	//根据角色的id 获取 角色-权限 映射表中的 所以权限
			  	List<PowerGroup> powerGroup = powerGroupService.selectPowersByRoles(roleIds);
			  	for (PowerGroup p : powerGroup) {
			  		powers.add(p.getPower());
				}
			  	List<Integer> ModuleIds=new ArrayList<>();
			  	//获取所有的权限
			  	List<Power> listPower = powerService.selectPowerByPowerStrs(powers);
			  	
			  	for (Power p : listPower) {
			  		ModuleIds.add(p.getModuleId());
				}
			  	
			  	//通过模块id获取所有的模块
			  	listModule = moduleService.selectModuleByModuleIds(ModuleIds);
			}

		  	//将所有的权限放到session域中
		  	map.put("listModule", listModule);
		  	
			
			return Msg.success().add("msg", "登录成功");
		}else{
			return Msg.fail().add("msg", "用户名或者密码不存在");
		}
	}
	
	/**
	 * 退出系统
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/loginOut",method=RequestMethod.GET)
	public String loginOut(Map<String,Object>map){
		map.clear();
		System.out.println(map.size());
		//map.remove("loginUser");
		return "login";
	}
}
